﻿using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Audio;
using Bejeweled.Support;
using Bejeweled.Manager;

namespace Bejeweled
{
    public enum GameState { Start, InGame, GameOver,About,GameWin };

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        ScreenManager screenManager;
        EntityManager entityManager;

        public GameState currentGameState = GameState.Start;

        Texture2D startScreen,startText,gameOver,gameWin;
        
        private SpriteFont font;

        float rotateSpeed = 0.01f;
        float rotation;

        Vector2 originstart,originabout,originquit;
        Texture2D buttonstart,buttonabout,buttonquit,buttonback,buttonPlayAgain,buttonQuitWin;
        Texture2D imageStart, imageAbout, imageQuit,imageWin,imageLose,imageYouLose,imageYouWin;

        //vẽ các texture của phần ABOUT
        Texture2D info, HTC;


        public EntityManager GetEntityManager { get { return entityManager; } }
        public ScreenManager GetScreenManager { get { return screenManager; } }

        int down = -200;
        int up = 0;
        int left = -400;
        int right = 1000;

        //khai báo Sound
        SoundEffect soundeffect;
        SoundEffectInstance soundeffectInstance;
        public SoundEffect soundClick;

        //vẽ ống điểm
        public Vector2 PointBar = Vector2.Zero;
       
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.SupportedOrientations = DisplayOrientation.Portrait|DisplayOrientation.LandscapeLeft|DisplayOrientation.LandscapeRight;
            //graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = true;
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            
        }

        protected override void Initialize()
        {
            TouchPanel.EnabledGestures =
               GestureType.Hold |
               GestureType.Tap |
               GestureType.DoubleTap |
               GestureType.FreeDrag |
               GestureType.Flick |
               GestureType.Pinch;

            screenManager = new ScreenManager(this);
            Components.Add(screenManager);
            screenManager.Enabled = false;
            screenManager.Visible = false;

            entityManager = new EntityManager(this);
            Components.Add(entityManager);
            entityManager.Enabled = false;
            entityManager.Visible = false;

            
            base.Initialize();
        }

        // HAM LOADCONTENT()
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            startScreen = Content.Load<Texture2D>("Images/startScreen");
            startText = Content.Load<Texture2D>("Images/bejewelStart");
            gameWin = Content.Load<Texture2D>("Images/winScreen");
            gameOver = Content.Load<Texture2D>("Images/loseScreen");
            

            font = Content.Load<SpriteFont>("FontsBarsFont");
            buttonstart = Content.Load<Texture2D>("Images/buttonStart");
            buttonabout = Content.Load<Texture2D>("Images/buttonAbout");
            buttonquit = Content.Load<Texture2D>("Images/buttonQuit");
            buttonback = Content.Load<Texture2D>("Images/buttonBack");
            buttonPlayAgain = Content.Load<Texture2D>("Images/playAgain");
            buttonQuitWin = Content.Load<Texture2D>("Images/QuitWin");

            imageStart = Content.Load<Texture2D>("Images/imageStart");
            imageAbout = Content.Load<Texture2D>("Images/imageAbout");
            imageQuit = Content.Load<Texture2D>("Images/imageQuit");

            imageWin = Content.Load<Texture2D>("Images/avatarwinScreen");
            imageYouWin = Content.Load<Texture2D>("Images/youWin");
            imageLose = Content.Load<Texture2D>("Images/avatarLoseScreen");
            imageYouLose = Content.Load<Texture2D>("Images/youLose");

            info = Content.Load<Texture2D>("Images/aboutScreen");
            HTC = Content.Load<Texture2D>("Images/WP7");

            soundeffect = Content.Load<SoundEffect>("Audios/soundground");
            soundClick = Content.Load<SoundEffect>("Audios/eat2");
            soundeffectInstance = soundeffect.CreateInstance();
            soundeffectInstance.IsLooped = true;


            originstart = new Vector2(buttonstart.Width / 2, buttonstart.Height/2);
            originabout = new Vector2(buttonabout.Width / 2, buttonabout.Height / 2);
            originquit = new Vector2(buttonquit.Width / 2, buttonquit.Height / 2);
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            rotation += rotateSpeed;
            
                switch (currentGameState)
                {
                    case GameState.Start:
                        soundeffectInstance.Play();
                        if (TouchPanel.IsGestureAvailable)
                        {
                            GestureSample gesture = TouchPanel.ReadGesture();
                        // we can use the type of gesture to determine our behavior
                            if (gesture.GestureType == GestureType.Tap &&  (gesture.Position.X > 190 && gesture.Position.X  < 280) && (gesture.Position.Y > 410 && gesture.Position.Y < 500))
                            {
                                soundClick.Play();
                                currentGameState = GameState.InGame;
                                entityManager.point = 30;
                                screenManager.Enabled = true;
                                screenManager.Visible = true;
                                entityManager.Enabled = true;
                                entityManager.Visible = true;
                                soundeffectInstance.Stop();
                            }                                               
                            else if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 50 && gesture.Position.X < 140) && (gesture.Position.Y > 570 && gesture.Position.Y < 660))
                            {
                                soundClick.Play();
                                down = -200;
                                currentGameState = GameState.About;
                            }
                            else if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 325 && gesture.Position.X < 415) && (gesture.Position.Y > 560 && gesture.Position.Y < 650))
                            {
                                soundClick.Play();
                                Exit();
                            }
                        }
                        
                        break;
                        
                    case GameState.About:
                        if (TouchPanel.IsGestureAvailable)
                        {
                            GestureSample gesture = TouchPanel.ReadGesture();
                            if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 325 && gesture.Position.X < 415) && (gesture.Position.Y > 560 && gesture.Position.Y < 650))
                            {
                                soundClick.Play();
                                currentGameState = GameState.Start;
                                up = 0 ;
                                down = -200;
                            }
                        }
                        break;
                        
                    case GameState.InGame:
                            screenManager.Enabled = true;
                            screenManager.Visible = true;
                            entityManager.Enabled = true;
                            entityManager.Visible = true;
                            if (entityManager.point < 1)
                            {
                                currentGameState = GameState.GameOver;
                            }
                            else if (entityManager.point >= 459)
                            {
                                currentGameState = GameState.GameWin;
                            }

                        break;

                    case GameState.GameOver:
                            screenManager.Enabled = false;
                            screenManager.Visible = false;
                            entityManager.Enabled = false;
                            entityManager.Visible = false;
                            if (TouchPanel.IsGestureAvailable)
                            {
                                GestureSample gesture = TouchPanel.ReadGesture();
                                if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 100 && gesture.Position.X < 205) && (gesture.Position.Y > 225 && gesture.Position.Y < 330))
                                {
                                    soundClick.Play();
                                    currentGameState = GameState.InGame;
                                    entityManager.point = 30;
                                    right = 1000;
                                }   
                                else if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 295 && gesture.Position.X < 405) && (gesture.Position.Y > 325 && gesture.Position.Y < 430))
                                {
                                    soundClick.Play();
                                    currentGameState = GameState.Start;                                   
                                    right = 1000;
                                }
                        }

                        break;

                    case GameState.GameWin:
                            screenManager.Enabled = false;
                            screenManager.Visible = false;
                            entityManager.Enabled = false;
                            entityManager.Visible = false;
                            if (TouchPanel.IsGestureAvailable)
                            {
                                GestureSample gesture = TouchPanel.ReadGesture();
                                if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 100 && gesture.Position.X < 205) && (gesture.Position.Y > 225 && gesture.Position.Y < 330))
                                {
                                    soundClick.Play();
                                    currentGameState = GameState.InGame;
                                    entityManager.point = 30;
                                    right = 1000;
                                }   
                                else if (gesture.GestureType == GestureType.Tap && (gesture.Position.X > 295 && gesture.Position.X < 405) && (gesture.Position.Y > 325 && gesture.Position.Y < 430))
                                {
                                    soundClick.Play();
                                    currentGameState = GameState.Start;                                   
                                    right = 1000;
                                }
                        }

                        break;

                    default:
                        currentGameState = GameState.Start;
                        break;
                
               // break;
            }

                    base.Update(gameTime);


        }

        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            switch (currentGameState)
            {
                case GameState.Start:
                    spriteBatch.Begin();
                    spriteBatch.Draw(startScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                    if (down <= -4)
                        down +=8;
                    spriteBatch.Draw(startText, new Rectangle(0, down, startText.Width, startText.Height), Color.White);
                    
                    spriteBatch.Draw(buttonstart, new Vector2(240, 460-down), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageStart, new Vector2(270, 490-down), null, Color.White, 0f, originstart,1.0f, SpriteEffects.None, 0.0f);

                    spriteBatch.Draw(buttonabout, new Vector2(100, 618-down), null, Color.White, rotation, originabout, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageAbout, new Vector2(130, 650-down), null, Color.White, 0f, originabout, 1.0f, SpriteEffects.None, 0.0f);

                    spriteBatch.Draw(buttonquit, new Vector2(380, 618-down), null, Color.White, rotation, originquit, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageQuit, new Vector2(405, 640-down), null, Color.White, 0f, originquit, 0.7f, SpriteEffects.None, 0.0f);

                    /*
                    spriteBatch.DrawString(font, "PLAY", new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2), Color.Red, 2f,Vector2.Zero, 1f, SpriteEffects.None, 1f);
                    spriteBatch.DrawString(font, "EXIT", new Vector2(400f, 200f), Color.White);
                     */
                    spriteBatch.End();
                    break;

                case GameState.About:
                     spriteBatch.Begin();

                    //---------------------tắt các hình trên màn hình menu chính --------------------------
                    spriteBatch.Draw(startScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                    if (up >= -500)
                        up -=8;
                    spriteBatch.Draw(startText, new Rectangle(0,0 + up, startText.Width, startText.Height), Color.White);

                    spriteBatch.Draw(buttonstart, new Vector2(240, 460 - up), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageStart, new Vector2(270, 490 - up), null, Color.White, 0f, originstart, 1.0f, SpriteEffects.None, 0.0f);

                    spriteBatch.Draw(buttonabout, new Vector2(100, 618 - up), null, Color.White, rotation, originabout, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageAbout, new Vector2(130, 650 - up), null, Color.White, 0f, originabout, 1.0f, SpriteEffects.None, 0.0f);

                    spriteBatch.Draw(buttonquit, new Vector2(380, 618 - up), null, Color.White, rotation, originquit, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(imageQuit, new Vector2(405, 640 - up), null, Color.White, 0f, originquit, 1.0f, SpriteEffects.None, 0.0f);
                    //------------------------------------------------------------------------------

                    //--------------------mở các màn hình tên ABOUT------------------------------------
                    spriteBatch.Draw(info, new Vector2(-500-up,60), null, Color.White, 0f, originstart,1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.Draw(HTC, new Vector2(1400 + up, 90), null, Color.White, 0f, originstart, 1.0f, SpriteEffects.None, 0.0f);

                    //spriteBatch.Draw(buttonback, new Vector2(380, 1200 + up), null, Color.White, rotation, originquit, 1.0f, SpriteEffects.None, 0.0f);
                    if (down <= -4)
                        down += 8;
                    spriteBatch.Draw(buttonback, new Vector2(380, 618 - down), null, Color.White, rotation, originquit, 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.End();
                    break;

                case GameState.InGame:
                    break;
     
                case GameState.GameOver:
                     spriteBatch.Begin();
                     spriteBatch.Draw(gameOver, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                     if (right >= 450)
                         right -= 30;
                     spriteBatch.Draw(imageLose, new Rectangle(right, 0, imageWin.Width, imageWin.Height), Color.White);
                     spriteBatch.Draw(imageYouLose, new Rectangle(50,- (right -450) , imageYouWin.Width, imageYouWin.Height), Color.White);
                     spriteBatch.Draw(buttonPlayAgain, new Vector2(150, right-150), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                     spriteBatch.Draw(buttonQuitWin, new Vector2(350,  right-50), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                     spriteBatch.End();
                    break;

                case GameState.GameWin:
                     spriteBatch.Begin();
                     spriteBatch.Draw(gameWin, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                     if (right >= 450)
                         right -= 30;
                     spriteBatch.Draw(imageWin, new Rectangle(right, 0, imageWin.Width, imageWin.Height), Color.White);
                     spriteBatch.Draw(imageYouWin, new Rectangle(50,- (right -450) , imageYouWin.Width, imageYouWin.Height), Color.White);
                     spriteBatch.Draw(buttonPlayAgain, new Vector2(150, right-150), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                     spriteBatch.Draw(buttonQuitWin, new Vector2(350,  right-50), null, Color.White, rotation, originstart, 1.0f, SpriteEffects.None, 0.0f);
                     spriteBatch.End();
                     break;
                default:
                    break;
            }
            base.Draw(gameTime);
        }
    }

}
